package com.u.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.u.character.Role;
import com.u.eventInfo.EventLog;
import com.u.tool.EventLogUtil;

/**
 * Servlet implementation class NewEventLog
 */
@WebServlet("/NewEventLog")
public class NewEventLog extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewEventLog() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");	
		String event_id = request.getParameter("event_id");
		HttpSession httpSession = request.getSession();
		Role role = (Role) httpSession.getAttribute("Role");
		String poster_name = role.getName();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date d = new Date();
		String date = format.format(d.getTime());
		String content = request.getParameter("context");
		System.out.println(content);
		String pic_addr = request.getParameter("pic_address");
		EventLog eventLog = new EventLog();
		eventLog.setContent(content);
		eventLog.setDate(date);
		eventLog.setEvent_id(event_id);
		eventLog.setPic_addr(pic_addr);
		eventLog.setPoster_name(poster_name);
		EventLogUtil.AddLog(eventLog);	
		response.sendRedirect("/r/Log.jsp?event_id="+event_id);
	}

}
